Ext.define('cfWeb.controller.appAudit.AppAuditController', {
    extend: 'Ext.app.Controller',
    views: ['appAudit.AppAuditView', 'appAudit.AppAuditCfWindow', 'appAudit.AppInfoView', 'appAudit.ServiceConfigListView'],
    stores: ['appAudit.AppAuditStore'],
    init: function () {
        this.control({
            'appAuditCfWindow': {
                onSubmit: this.submitForm,
                beforeNext: this.formValidate
            }
        });
    },
    submitForm: function(me) {
        var window = me;
        var values = window.down('form[name=ServiceConfigList.Form]').getForm().getValues();
        console.log(values);
        for (var key in values) {
            if (key.endsWith('.service') && values[key] && !values[key.split('.')[0] + '.plan']) {
                Ext.Msg.alert('提示', '必须选择' + key.split('.')[0] + '的servicePlan');
                return;
            }
        }
        Ext.Msg.show({
            icon: Ext.Msg.QUESTION,
            title: '确定操作',
            msg: '是否确定批准' + me.data.userName + '的申请？',
            timeout: 1800000,
            buttons: Ext.Msg.YESNO,
            fn: function (btn, text) {
                if (btn == 'yes') {
                    var waitWindow = Ext.MessageBox.show({
                        title: cfWebLang.Util.CreateApp,
                        closable: false,
                        cls: 'style-cfLoading'
                    });
                    Ext.Ajax.request({
                        url: 'rest/audit/agree?id=' + me.data.id,
                        params: values,
                        method: 'POST',
                        success: function (res) {
                            var text = JSON.parse(res.responseText);
                            if (text.success === true) {
                                me.auditStore.load();
                                Ext.Msg.alert('提示', text.data);
                                window.close();
                            } else {
                                Ext.Msg.alert('提示', text.error);
                            }
                            waitWindow.close();
                        },
                        failure: function (res) {
                            Ext.Msg.alert('提示', '请求失败，服务器返回 ' + res.status);
                            waitWindow.close();
                        }
                    });
                }
            }
        });
    },
    formValidate: function(me, oldIndex, newIndex) {
        return true;
    }
});
